import {Component, OnInit} from '@angular/core';
import {NavController} from 'ionic-angular';
// import {NewCreatPage} from "../newcreat/newcreat";
import {CollectPage} from "../collect/collect";
// import {ResultPage} from "../result/result";
import {ManagementPage} from "../management/management";
import {services} from "../../../providers/service";
import {StorageService} from "./storage.service";
import {Http} from "@angular/http";
import {NewCreatPage} from "../newcreat/newcreat";


@Component({
    selector: 'storage',
    templateUrl: 'storage.html',
    providers: [StorageService]
})
export class StoragePage implements OnInit {
    activeModelText = "当前扫码进度将会保留<br/>";
    hasStorage = true;                    //设置库存是否为空
    isShowEditModel = false;              //更多操作的弹框
    showAlert = true;                     //控制弹框的显隐
    myItems = [];
    index;                                //遍历的索引值

    constructor(public navCtrl: NavController,
                public service: services,
                public http: Http,
                public storageService: StorageService) {

    }

    ngOnInit() {
        this.service.activeModelText = this.activeModelText;
        this.storageService.outList()
            .then(data => {
                // this.myItems = data;
                this.myItems = this.storageService.items;
            }, err => {
                console.log('错误', err);
            })
            .catch(fail => {
                console.log('异常', fail);
            });
    }

    ToNewcreat() {
        this.navCtrl.push(NewCreatPage);
    }

    ToCollectPage() {
        this.navCtrl.setRoot(CollectPage);
    }

    // 完成按钮 -- 跳转到确认出库页面
    ToManagement() {
        this.navCtrl.push(ManagementPage);
    }

    //更多操作
    showEditModel($event, idx) {
        console.log($event, idx);
        this.index = idx;
        this.isShowEditModel = true;
    }

    toShowAlert() {
        this.showAlert = false;
    }

    btnCancel() {
        this.showAlert = true;
        this.isShowEditModel = false;
    }

    btnChoose() {
        this.showAlert = true;
        this.isShowEditModel = false;
        this.navCtrl.setRoot(StoragePage);
    }

    // -- 更多操作 -- 删除一箱
    deleteBox() {
        console.log('去扫码');
        this.isShowEditModel = false;
    }

    //下拉刷新
    doRefresh(refresher) {
        console.log('Begin async operation', refresher);
        this.storageService.outList()
            .then(data => {
                console.log(data);
                if (data[0].resultStatus == 'success') {
                    this.myItems = data[0].infoData.items;
                    refresher.complete();
                }
            }, err => {
                console.log('错误', err);
            })
            .catch(fail => {
                console.log('异常', fail);
            });
    }

    //上拉加载更多
    doInfinite(infiniteScroll) {
        console.log('Begin async operation');
        this.storageService.outList()
            .then(data => {
                console.log(data);
                if (data[0].resultStatus == 'success') {
                    this.myItems = this.myItems.concat(data[0].infoData.items);
                    infiniteScroll.complete();
                }
            }, err => {
                console.log('错误', err);
            })
            .catch(fail => {
                console.log('异常', fail);
            });
    }


}
